#include "CLoginRewardMgr.h"
#include "MsgAct.pb.h"
#include "../mainctrl.h"
#include "../act/CActMgr.h"
#include "../data/CPlayerOnlineData.h"
#include "../cmd/dataprepare.h"
#include "../cmdinner/CCmdInnerActTitle.h"

void CLoginRewardMgr::UpdateData(CPlayerDetailInfoEx1& stPlayerDetailEx1,
        bool& bUpdateToDB)
{
    bUpdateToDB = false;

    if (CMainCtrl::m_pstMain->m_iDayZeroTime <=
            stPlayerDetailEx1._stLoginReward._iOpenZeroTime)
    {
        return;
    }

    char chCharMask = 0;

    for (int i = 0; i < MAX_LOGIN_REWARD_DAY_COUNT; ++i)
    {
        chCharMask = (char)((char)0x1 << i);
        if ((stPlayerDetailEx1._stLoginReward._chLoginMask & chCharMask))//注意只能判断0和非0两种情况
        {
            continue;
        }

        stPlayerDetailEx1._stLoginReward._chLoginMask |= chCharMask;
        stPlayerDetailEx1._stLoginReward._iOpenZeroTime =
            CMainCtrl::m_pstMain->m_iDayZeroTime;
        bUpdateToDB = true;
        break;
    }

    if (bUpdateToDB)
    {
        CMainCtrl::m_pstMain->m_stRewardBackMgr.UpdateDetailEx1ToDB(
            stPlayerDetailEx1);
    }
}

//获取信息
int CLoginRewardMgr::GetInfo(CPlayerDetailInfoEx1& stPlayerDetailEx1,
        MsgAnsLoginReward& stAns)
{
#define MACRO_LOGIN_REWARD_CAN_NOT 0    //未达到领取条件
#define MACRO_LOGIN_REWARD_CAN_GET 1    //可领取
#define MACRO_LOGIN_REWARD_ALREADY 2    //已领取

    char chCharMask = 0;

    for (int i = 0; i < MAX_LOGIN_REWARD_DAY_COUNT; ++i)
    {
        chCharMask = (char)((char)0x1 << i);
        if ((stPlayerDetailEx1._stLoginReward._chLoginMask & chCharMask))
        {
            if ((stPlayerDetailEx1._stLoginReward._chRewardMask & chCharMask))
            {
                stAns.add_mode_list(MACRO_LOGIN_REWARD_ALREADY);
            }
            else
            {
                stAns.add_mode_list(MACRO_LOGIN_REWARD_CAN_GET);
            }
        }
        else
        {
            stAns.add_mode_list(MACRO_LOGIN_REWARD_CAN_NOT);
        }
    }

    return 0;
}

//领取奖励
int CLoginRewardMgr::GetAward(CPlayerDetailInfoEx1& stPlayerDetailEx1,
        CPlayerOnlineData& stPlayerOnline, MsgReqLoginReward& stReq,
        MsgAnsLoginReward& stAns)
{
    if (!stReq.has_award_index())
    {
        CT_WARNING((""));
        return RET_REQ_NOT_COMPLETE;
    }

    int iRet = 0;
    int iIndex = 0;
    int iDayIndex = stReq.award_index();//第x天奖励 值域[1,7]避免使用默认值0

    if (iDayIndex <= 0 || iDayIndex > MAX_LOGIN_REWARD_DAY_COUNT)
    {
        return RET_INVALID_PARAM;
    }

    iIndex = CMainCtrl::m_pstMain->m_stDayWishMgr.FindConfig(iDayIndex);
    if (iIndex < 0)
    {
        return ERROR_SYS_CONFIG_EMPTY;
    }

    CDayWishCfgData& stCfgData = 
        CMainCtrl::m_pstMain->m_stDayWishMgr.GetConfig(iIndex);

    char chCharMask = 0;

    chCharMask = (char)((char)0x1 << (iDayIndex - 1));
    if (!(stPlayerDetailEx1._stLoginReward._chLoginMask & chCharMask))
    //注意只能判断0和非0两种情况
    {
        return ERROR_LOGIN_REWARD_UNFIT_TARGET;
    }

    if ((stPlayerDetailEx1._stLoginReward._chRewardMask & chCharMask))
    //注意只能判断0和非0两种情况
    {
        return ERROR_LOGIN_REWARD_GET_ALREADY;
    }

    CDropItem stDropItem;
    for (int i = 0; i < stCfgData.GetItemSize(); ++i)
    {
        CDropItemData& stItemData = stCfgData.GetLoginItem(i);
        stDropItem.DropItem(stItemData._usItemConfigID, stItemData._ucItemType,
            stItemData._iItemCount);
    }

    if (CMainCtrl::m_pstMain->m_stItemMgr.CheckBagFull(
            stPlayerOnline._stUserStorage, stDropItem))
    {
        return ERROR_STORAGE_BAG_IS_FULL;
    }

    //设置已领取
    stPlayerDetailEx1._stLoginReward._chRewardMask |= chCharMask;

    //发送物品
    stDropItem.SetOssModule(OSS_MODULE_LOGIN_REWARD);
    iRet = CMainCtrl::m_pstMain->m_stItemMgr.DropItem(NULL,
        stPlayerDetailEx1._uiGID, stDropItem);
    if (iRet)
    {
        CT_ERROR(("Role(%u) Drop Day Index(%d) Error(%d)",
            stPlayerDetailEx1._uiGID, iDayIndex, iRet));
    }
    else
    {
        for (int i = 0; i < stCfgData.GetItemSize(); ++i)
        {
	        MsgGridItemInfo* pstMsgGridItem = stAns.add_award_items();
            if (!pstMsgGridItem){CT_ERROR(("MsgGridItemInfo Null!"));continue;}

            CDropItemData& stItemData = stCfgData.GetLoginItem(i);

            pstMsgGridItem->set_item_type(stItemData._ucItemType);
            pstMsgGridItem->set_item_config_id(stItemData._usItemConfigID);
            pstMsgGridItem->set_item_count(stItemData._iItemCount);
        }
    }

    return GetInfo(stPlayerDetailEx1, stAns);
}

void CLoginRewardMgr::NotifyAllOnlineUsers()
{
    CCmdInnerActTitle* pstCmd =
        (CCmdInnerActTitle*)CT_CMDFACTORY->CreateCmd(
            ID_MAIN_INNER_ACT_TITLE);
    if (!pstCmd) {CT_ERROR(("Create CCmdInnerActTitle error!")); return;}
    
    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->_uiGID = 0;
    pstCmd->_iSubCmd = CActMgr::CMD_ACT_OPT_SUB_NOTIFY_LOGIN_REWARD;
    pstCmd->_iIndex = 0;
    CMainCtrl::m_pstMain->m_stPlayerOnline.GetAllOnlineUser(pstCmd->_auiAllGID);
    pstCmd->Do(NULL);
}

